﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewViews.Models;
using NewViews.ViewModels;

namespace NewViews.Controllers
{
    public class ShoppingCartController :Controller
    {
        EShopDataEntities5 storeDB = new EShopDataEntities5();
        //
        // GET: /ShoppingCart/
        public ActionResult Index()
        {
             object value = Session["key"];
             if (value != null)
             {

                 var cart = ShoppingCart.GetCart(this.HttpContext);
                 // Set up our ViewModel
                 var viewModel = new ShoppingCartViewModel
                 {
                     CartItems = cart.GetCartItems(),
                     CartTotal = cart.GetTotal()
                 };
                 // Return the view
                 return View(viewModel);
             }
             return RedirectToAction("Login", "Account");
        }
        //
        // GET: /Store/AddToCart/5
        public ActionResult AddToCart(int id)
        {
             object value = Session["key"];
             if (value != null)
             {

            // Retrieve the album from the database
            var addedAlbum = storeDB.Products
            .Single(album => album.Product_Id == id);
            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.AddToCart(addedAlbum);
            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
             }
             return RedirectToAction("Login", "Account");

        }
        //
        // AJAX: /ShoppingCart/RemoveFromCart/5
        //public ActionResult RemoveFromCart(int id = 0)
        //{
        //    return View();
        //}
        //[HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            
             object value = Session["key"];
             if (value != null)
             {
            //Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            int itemCount = cart.RemoveFromCart(id);
            return View();
             }
             return RedirectToAction("Login", "Account");

        }
        public ActionResult CheckOut()
        {
            
             object value = Session["key"];
             if (value != null)
             {
            return View();
             }
             return RedirectToAction("Login", "Account");
        }
        public ActionResult Details(int id = 0)
        {
            object value = Session["key"];
            if (value != null)
            {
                
                var products = storeDB.Products;
                ViewBag.prod = products.Find(id);
                Product dbPprod = products.Find(id);
                if (dbPprod == null)
                {
                    return HttpNotFound();
                }
                return View(dbPprod);
            }
            return RedirectToAction("Login", "Account");

        }
//      
//public ActionResult  RemoveFromCart(int id)
//{
////Remove the item from the cart
//var cart = ShoppingCart.GetCart(this.HttpContext);
//// Get the name of the album to display confirmation
//string albumName = storeDB.Carts.Single(item => item.RecordId == id).Product.Product_Name;
//// Remove from cart
//int itemCount = cart.RemoveFromCart(id);
//// Display the confirmation message
//var results = new ShoppingCartRemoveViewModel
//{
//Message = Server.HtmlEncode(albumName) +
//" has been removed from your shopping cart.",
//CartTotal = cart.GetTotal(),
//CartCount = cart.GetCount(),
//ItemCount = itemCount,
//DeleteId = id
//};
//    return Json(results);
//}
//return View();
// GET: /ShoppingCart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            object value = Session["key"];
            if (value != null)
            {
                var cart = ShoppingCart.GetCart(this.HttpContext);
                ViewData["CartCount"] = cart.GetCount();
                return PartialView("CartSummary");
            }
            return RedirectToAction("Login", "Account");
        }
    }
}